<template>
  <AMISRenderer :schema="schema" />
</template>

<script setup>
import AMISRenderer from '@/components/AMISRenderer.vue'

let source = {
  url: '/base/region/province',
  adaptor: function (payload, response, api, context) {
    console.log(payload);
    if(payload.data){
      for(let i=0;i<payload.data.length;i++){
        payload.data[i].defer = true;
      }
    }
    return {
        ...payload
    };
  }
}

let deferApi = {
  url: '/base/region/children',
  adaptor: function (payload, response, api, context) {
    if(payload.data){
      for(let i=0;i<payload.data.length;i++){
        payload.data[i].defer = true;
      }
    }
    return {
        ...payload
    };
  },
  data:{
    parentId: '${id}'
  }
}

const schema = {
    type: 'page',
    body:{
        type: 'crud',
        name: 'area-list',
        id: 'area-list',
        autoFillHeight: true,
        syncLocation: false,
        api: '/base/district/list',
        footerToolbar: [
          'statistics',
          'switch-per-page',
          'pagination'
        ],
        autoGenerateFilter: {
          showBtnToolbar: false,
          columnsNum: 3
        },
        headerToolbar:[
          {
            type: 'button',
            actionType: 'dialog',
            align: 'right',
            label: '新增',
            icon: 'fa fa-plus pull-left',
            primary: true,
            dialog: {
              title: '新增行政区',
              closeOnEsc: true,
              showErrorMsg: false,
              body:{
                type: 'form',
                api:'post:/base/district/add',
                canAccessSuperData: false,
                messages:{
                  validateFailed:''
                },
                body:[
                  {
                    type: 'input-text',
                    name: 'regionName',
                    required: true,
                    label: '行政区名称',
                    maxLength: 50,
                    validationErrors: {
                      isRequired: '请输入行政区名称'
                    }
                  },
                  {
                    type: 'input-text',
                    name: 'regionCode',
                    required: true,
                    maxLength: 20,
                    label: '行政区编码',
                    validationErrors: {
                      isRequired: '请输入行政区编码'
                    }
                  },
                  {
                    type: 'button-group-select',
                    name: 'level',
                    label: '级别',
                    source: '/base/dict/options/region-level',
                    required: true,
                    validationErrors: {
                      isRequired: '请选择级别'
                    }
                  },
                  {
                    type: 'tree-select',
                    name: 'parentId',
                    label: '上级行政区',
                    labelField: 'regionName',
                    valueField: 'id',
                    initiallyOpen: false,
                    unfoldedLevel: 0,
                    deferApi: deferApi,
                    source: source,
                  },
                ],
              },
            }
          },
          {
            type: 'button',
            actionType: 'dialog',
            align: 'right',
            label: '预览',
            dialog:{
              title: false,
              closeOnEsc: true,
              actions: [
                {
                  type: 'button',
                  actionType: 'close',
                  label: '关闭',
                  level: 'primary'
                }
              ],
              body: {
                type: 'input-tree',
                  name: 'regionCode',
                  source: '/base/district/tree',
                  label: false,
                  labelField: 'regionName',
                  valueField: 'regionCode',
                  initiallyOpen: false,
                  unfoldedLevel: 0,
                  deferApi: deferApi,
                  source: source,
              }
            }
          }
        ],
        columns: [
            {
              name: 'regionName',
              label: '行政区名称',
              searchable: {
                type: 'input-text',
                name: 'regionName',
                label: '行政区名称',
                clearable: true
              }
            },
            {
              name: 'regionCode',
              label: '行政区编码',
              searchable: {
                type: 'input-text',
                name: 'regionCode',
                label: '行政区编码',
                clearable: true
              }
            },
            {
                name: 'levelStr',
                label: '级别'
            },
            {
              type: 'operation',
              label: '操作',
              buttons: [
                {
                  label: '修改',
                  type: 'button',
                  actionType: 'dialog',
                  dialog: {
                    title: '修改行政区',
                    closeOnEsc: true,
                    showErrorMsg: false,
                    body:{
                      type: 'form',
                      api:'post:/base/district/update',
                      messages:{
                        validateFailed:''
                      },
                      body:[
                        {
                          type: 'hidden',
                          name: 'id',
                        },
                        {
                          type: 'input-text',
                          name: 'regionName',
                          required: true,
                          maxLength: 50,
                          label: '行政区名称'
                        },
                        {
                          type: 'input-text',
                          name: 'regionCode',
                          required: true,
                          label: '行政区编码',
                          maxLength: 20,
                        },
                        {
                          type: 'button-group-select',
                          name: 'level',
                          label: '级别',
                          source: '/base/dict/options/region-level',
                          required: true,
                          validationErrors: {
                            isRequired: '请选择级别'
                          }
                        },
                        {
                          type: 'tree-select',
                          name: 'parentId',
                          label: '上级行政区',
                          labelField: 'regionName',
                          valueField: 'id',
                          initiallyOpen: false,
                          unfoldedLevel: 0,
                          deferApi: deferApi,
                          source: source,
                        },
                      ]
                    },
                  },
                },
                {
                  label: '删除',
                  type: 'button',
                  level: 'danger',
                  actionType:'ajax',
                  confirmText: '确认要删除该记录？',
                  api: {
                    url: '/base/district/delete?id=${id}',
                    method: 'post',
                    data:{}
                  },
                },
              ]
            }
        ]
    }
}
</script>
